<template>
  <transition name='slide'>
      <music-list
        :title="title"
        :bg-image="bgImage"
        :songs="songs"
        @selectItem="selectItem"
      ></music-list>
  </transition>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import musicList from 'comp/music-list/musicList';
import { getSongList } from '@/api/recommend';
import { createSong } from '@/common/js/song';

export default {
  name: 'disc',
  data() {
    return {
      songs: [],
    };
  },
  computed: {
    title() {
      return this.disc.creator.name || '';
    },
    bgImage() {
      return this.disc.imgurl;
    },
    ...mapGetters(['disc']),
  },
  methods: {
    selectItem({ index }) {
        const list = this.songs;
        this.selectPlay({ list, index });
    },
    _getSongList() {
      if (!this.disc.dissid) {
        this.$router.push('/recommend');
        return;
      }
      getSongList(this.disc.dissid).then((res) => {
        this.songs = this._normalizeSongs(res.cdlist[0].songlist);
      });
    },
    _normalizeSongs(list) {
      const ret = [];
      list.forEach((musicData) => {
        if (musicData.songid && musicData.albummid) {
          ret.push(createSong(musicData));
        }
      });
      return ret;
    },
    ...mapActions([
        'selectPlay',
        'randomPlay',
    ]),
  },
  created() {
    this._getSongList();
  },
  components: {
    musicList,
  },
};
</script>
<style lang='stylus' scoped>
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
